//
//  KPFirstViewController.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/3/21.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit
import FSPagerView
import TangramKit
import Alamofire
import RxAlamofire

class KPFirstViewController: KPBaseTableViewController {

    fileprivate var scrollPageImage: FSPagerView!
    fileprivate var dataSource = [FirstCellModel]()
    fileprivate var localBtn: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareView()
    }

    // MARK: - 创建视图
    fileprivate func prepareView() {

        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }

        kp_setNavigationBarColor(color: .clear, tintColor: .white)

        let liner = TGLinearLayout(.vert)
        liner.tg_width.equal(.fill)
        liner.tg_height.equal(.wrap)

        scrollPageImage = FSPagerView().then {
            $0.dataSource = self
            $0.delegate = self
            $0.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "FSPagerViewCell")
            $0.tg_width.equal(.fill)
            $0.tg_height.equal(200)
            $0.automaticSlidingInterval = 2.5
            $0.isInfinite = true
            liner.addSubview($0)
        }
        liner.layoutIfNeeded()
        tableView.tableHeaderView = liner
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.register(cellWithClass: KPFirstTableViewCell.self)
        tableView.ly_emptyView = k_emptyView(target: self, selector: #selector(testDatas))
        tableView.ly_emptyView.contentViewOffset = 100

        let localBtn = UIBarButtonItem.init(title: "定位", style: UIBarButtonItemStyle.plain, target: self, action: #selector(localBtnClick(btn:)))
        self.navigationItem.leftBarButtonItem = localBtn



    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.height = Screen.height - kTabbarH
    }

    func testAction() {

        _ = KPButton.init(image: #imageLiteral(resourceName: "首页1"), title: "push").then {
            $0.imagePosistion = .top
            $0.titleImageSpacing = 2
            $0.imageSize = 30.size // 设置图片大小
            $0.setTitleColor(UIColor.Material.blue500, for: .normal)
            $0.titleLabel?.font = 16.font
            view.addSubview($0)
            $0.center = view.center
            $0.bounds = CGRect.init(x: 0, y: 0, width: 80, height: 60)
            $0.addTarget(for: .touchUpInside, action: { _ in
                let vc = TestViewController()
                self.navigationController?.pushViewController(vc)
            })
        }
    }

    @objc func localBtnClick(btn: UIBarButtonItem) {
        let vc = KPChooseCityViewController()
        vc.selectCityClosure = {
            btn.title = $0
        }
        self.navigationController?.pushViewController(vc)
    }

    @objc
    func testDatas() {
        for index in 1...10 {
            let model = FirstCellModel.init(headImage: "", name: "我是名字\(index)", time: Date.random().string(withFormat: "YYYY-MM-dd HH:mm:ss"), detail: String.random(ofLength: index * 50))
            dataSource.append(model)
            KPLog("model")

        }
        tableView.reloadData()
    }

}

// MARK: - 轮播图代理
extension KPFirstViewController: FSPagerViewDataSource, FSPagerViewDelegate {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return 3
    }

    // MARK: - 自动缓存高度
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "FSPagerViewCell", at: index)
        cell.imageView?.image = UIImage.init(color: UIColor.random, size: CGSize.init(width: Screen.width, height: 200))
        cell.textLabel?.text = "我是标题\(index)"

        return cell
    }
}

// MARK: - UITableView 代理
extension KPFirstViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cell = self.tableView(tableView, cellForRowAt: indexPath) as! KPFirstTableViewCell
        let size = cell.rootLayout.tg_sizeThatFits(CGSize.init(width: Screen.width, height: 0))
        return size.height
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: KPFirstTableViewCell.self)
        cell?.setModel(dataSource[indexPath.row])
        return cell!
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = TestViewController()
        self.navigationController?.pushViewController(vc)
    }
}

extension KPFirstViewController {
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let alpha = scrollView.contentOffset.y / kNavibarH

        kp_setNavigationBarScrollColor(color: UIColor.init(hex: 0x2196F3, transparency: alpha)!, tintColor: .white)

    }
}
